<?php
class track_controller extends app_controller {
  function pixel() {
    load('track');
    
    if (isset($_GET['r']) && preg_match('/search\?q=([^&]+)/', $_GET['r'], $matches)) {
      $search = strtr(urldecode($matches[1]), array('+' => ' '));
    }
    else {
      $search = NULL;
    }

    $track = new track;
    $track->save(array(
      'session_id' => session_id(),
      'url' => $this->args[0],
      'referer' => isset($_GET['r']) ? $_GET['r'] : NULL,
      'user_agent' => $_SERVER['HTTP_USER_AGENT'],
      'ip_address' => $_SERVER['REMOTE_ADDR'],
      'search' => $search,
    ));
    
    // http://www.zedwood.com/article/90/php-web-bug-transparent-gif-tracking
    header("content-type: image/gif");
    //43byte 1x1 transparent pixel gif
    echo base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
    
    $this->rendered = TRUE; // no template needed
  }
}